Parse.Cloud.define('createQlMember', async (req, res) => {
  const { userId, headimgurl, nickname, sex, province, city, country, openid } = req.params
  if (!userId) {
    return res.error('没有该用户')
  }
  if (!openid) {
    return res.error('请先授权登录')
  }

  try {
    const user = await new Parse.Query(Parse.User).equalTo('objectId', userId).first()
    if (!user) {
      return res.error('没有该用户')
    }

    const current = await new Parse.Query('QlMember').equalTo('user', user).first() || new Parse.Object('QlMember')
    const member = await current.save({
      user,
      headimgurl,
      nickname,
      sex,
      province,
      city,
      country,
      openid
    })
    res.success(member)
  } catch (err) {
    console.log(err.message)
  }
})
